<?php
namespace app\index\model;

use think\Db;

/**
 * 文章模型
 *
 * @author leolei <346991581@qq.com>
 */
class YogaArticle
{
    
    /**
     * 获取列表
     *
     * @param array $where 查询条件
     * @param string $order 排序参数
     * @param integer $p 页码
     * @param integer $limit 分页限制
     * @return void
     * @author leo.lei(346991581@qq.com)
     */
    public function getlist($where = [], $order = 'id desc', $p = 1, $limit = 10)
    {
        $where['is_deleted'] = 0;
        if ($p > 0) {
            $list = Db::name('YogaArticle')->where($where)->page($p, $limit)->order($order)->select();
        } else {
            $list = Db::name('YogaArticle')->where($where)->limit($limit)->order($order)->select();
        }
        return $list;
    }

    /**
     * 获取详情
     *
     * @param array $where
     * @return void
     * @author leo.lei(346991581@qq.com)
     */
    public function getinfo($where = [])
    {
        $info = Db::name('YogaArticle')->where($where)->find();
        return $info;
    }

    /**
     * 获取新闻列表
     *
     * @param array $where
     * @param string $order
     * @param integer $p
     * @param integer $limit
     * @return void
     * @author leo.lei(346991581@qq.com)
     */
    public function newslist($where = [], $order = 'id desc', $p = 1, $limit = 10)
    {
        $info['list'] = self::getlist($where, $order, $p, $limit);
        $info['page'] = self::pageinfo($where, $p, $limit);
        return $info;
    }

    /**
     * 分页信息
     *
     * @param array $where
     * @return void
     * @author leo.lei(346991581@qq.com)
     */
    protected function pageinfo($where, $p, $limit)
    {
        $count = Db::name('YogaArticle')->where($where)->count();
        $max = ceil($count/$limit);
        $page['count'] = $count;
        $page['limit'] = $limit;
        $page['p'] = $p;
        $page['max'] = $max;
        if ($p -1 >0) {
            $page['prev'] = $p -1;
        } else {
            $page['prev'] = '#';
        }
        if ($p + 1 >= $max) {
            $page['next'] = '#';
        } else {
            $page['next'] = $p + 1;
        }
        return $page;
    }
}
